from selenium import webdriver
import os
import time


class TestCase:
    def __init__(self):
        self.drvier = webdriver.Chrome()
        # 动态获取完整地址
        self.resource_path = os.path.abspath('resource')
        self.A_path = os.path.join(self.resource_path, 'A.html')

        # 打开浏览器
        self.drvier.get(self.A_path)

    def moot(self):
        print(self.drvier.find_element_by_xpath('//body/h1').get_attribute('outerHTML'))
        print(self.drvier.find_element_by_css_selector('h1').get_attribute('outerHTML'))

    def see_related_post(self):
        print(self.drvier.find_element_by_link_text('See related post').get_attribute('outerHTML'))
        print(self.drvier.find_element_by_css_selector('a').get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath('//p/a').get_attribute('outerHTML'))

    def drag_me(self):
        print(self.drvier.find_element_by_id('dragger').get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath('//body/div').get_attribute('outerHTML'))

    def item1(self):
        print(self.drvier.find_element_by_class_name('item1').get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//div[contains(text(),'1')]").get_attribute('outerHTML'))

    def item2(self):
        print(self.drvier.find_element_by_class_name('item2').get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//div[contains(text(),'Item 2')]").get_attribute('outerHTML'))

    def item3(self):
        print(self.drvier.find_element_by_class_name('item3').get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//div[contains(text(),'3')]").get_attribute('outerHTML'))

    def item4(self):
        print(self.drvier.find_element_by_class_name('item4').get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//div[contains(text(),'4')]").get_attribute('outerHTML'))

    def pppp(self):
        print(self.drvier.find_element_by_xpath("//p[contains(text(),'pppp')]").get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//body/p[@class='item1']").get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//p[2]").get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//p[contains(@class,'item1')]").get_attribute('outerHTML'))
        print(self.drvier.find_element_by_css_selector('p.item1').get_attribute('outerHTML'))

    def div_sub1(self):
        print(self.drvier.find_element_by_xpath("//div[contains(text(),'sub1')]").get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//div/div[1]").get_attribute('outerHTML'))
    def div_sub2(self):
        print(self.drvier.find_element_by_xpath("//div[contains(text(),'sub2')]").get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//div/div[2]").get_attribute('outerHTML'))
    def span1(self):
        print(self.drvier.find_element_by_xpath("//span[contains(text(),'span1')]").get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//body/div[6]/p[1]/span").get_attribute('outerHTML'))
    def span2(self):
        print(self.drvier.find_element_by_xpath("//body/div[6]/p[2]/span").get_attribute('outerHTML'))
        print(self.drvier.find_element_by_xpath("//span[contains(text(),'span2')]").get_attribute('outerHTML'))


    def quit(self):
        time.sleep(1)
        self.drvier.quit()


case = TestCase()

# case.moot()
# case.see_related_post()
# case.drag_me()
# case.item1()
# case.item2()
#case.item3()
# case.item4()

# case.pppp()
# case.div_sub1()
# case.div_sub2()
# case.span1()
case.span2()


case.quit()
